/*
 * Copyright (c) 2020 NVI, Inc.
 *
 * This file is part of VLBI Field System
 * (see http://github.com/nvi-inc/fs).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
/* k4 rec_mode snap command */

#include <stdio.h> 
#include <string.h> 
#include <sys/types.h>

#include "../include/params.h"
#include "../include/fs_types.h"
#include "../include/fscom.h"         /* shared memory definition */
#include "../include/shm_addr.h"      /* shared memory pointer */

void k4rec_mode(command,itask,ip)
struct cmd_ds *command;                /* parsed command structure */
int itask;
int ip[5];                           /* ipc parameters */
{
      int ilast, ierr, ichold, i, count;
      char *ptr;
      struct k4rec_mode_cmd lclc;
      int ivc;
      int isetup;

      char *arg_next();

      void k4rec_mode_dis();

      void skd_run(), skd_par();      /* program scheduling utilities */

      ichold= -99;                    /* check vlaue holder */

      ip[0]=ip[1]=0;

      isetup=-1;          /* 1 for DFC2100 reset; -1 for no reset */

      if(!(shm_addr->equip.drive[0] == K4 &&
	   (shm_addr->equip.drive_type[0] == K42 ||
	    shm_addr->equip.drive_type[0] == K42DMS) )) {
	ierr=-301;
	goto error;
      }
	  

      if (command->equal != '=') {            /* read module */
	k4rec_mode_req_q(ip);
	goto k4con;
      } 
      else if (command->argv[0]==NULL) goto parse;  /* simple equals */
      else if (command->argv[1]==NULL) /* special cases */
        if (*command->argv[0]=='?') {
          k4rec_mode_dis(command,itask,ip);
          return;
        }

/* if we get this far it is a set-up command so parse it */

parse:
      isetup=1;               /* This is a set-up command for DFC2100 */
      ilast=0;                                      /* last argv examined */
      memcpy(&lclc,&shm_addr->k4rec_mode,sizeof(lclc));

      count=1;
      while( count>= 0) {
        ptr=arg_next(command,&ilast);
        ierr=k4rec_mode_dec(&lclc,&count, ptr);
        if(ierr !=0 ) goto error;
      }

/*  If command parameters are the same as those on shared memory &&    */
/*  previous execution has succeeded, terminate this set-up and        */
/*  emulate read module.                                               */

      if (memcmp(&shm_addr->k4rec_mode,&lclc,sizeof(lclc))==0
        && shm_addr->k4rec_mode_stat==1) {
        ip[0]=ip[1]=0;
        k4rec_mode_req_q(ip);
        command->equal=0;
	command->argv[0]=command->argv[1]=NULL;
        goto k4con;
      }

/* all parameters parsed okay, update common */

      ichold=shm_addr->check.k4rec.check;
      shm_addr->check.k4rec.check=0;

      memcpy(&shm_addr->k4rec_mode,&lclc,sizeof(lclc));

/* format buffers for k4con */

      k4rec_mode_req_c(ip,&lclc);

k4con:
      skd_run("ibcon",'w',ip);
      skd_par(ip);
      if (isetup==1) {
        if (ip[2]==0) {
          shm_addr->k4rec_mode_stat=1; /* Succeed */
        } else {
          shm_addr->k4rec_mode_stat=-1; /* Failure */
        }
      }
      if (ichold != -99) {
	shm_addr->check.k4rec.state=TRUE;
	if (ichold >= 0)
	  ichold=ichold % 1000 + 1;
	shm_addr->check.k4rec.check=ichold;
      }

      if(ip[2]<0) {
	cls_clr(ip[0]);
	ip[0]=ip[1]=0;
	return;
      }

      k4rec_mode_dis(command,itask,ip);
      return;

error:
      ip[0]=0;
      ip[1]=0;
      ip[2]=ierr;
      memcpy(ip+3,"km",2);
      return;
}
